//
//  SendCommentView.m
//  产业头条
//
//  Created by ZJ on 16/9/26.
//  Copyright © 2016年 ZJ. All rights reserved.


//发表评论的View

#import "SendCommentView.h"

#import "ZJAdditions.h"
@interface SendCommentView ()<UITextViewDelegate>
{
    CGFloat _width;
    CGFloat _height;
}

/** 发表 */
@property (nonatomic, strong) UIButton *send;
@end

@implementation SendCommentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _width = frame.size.width;
        _height = frame.size.height;
        self.backgroundColor = [UIColor colorWithRed:0.8968 green:0.8968 blue:0.8968 alpha:1.0];
        [self addSubview:self.commentTextView];
        [self addSubview:self.send];
    }
    return self;
}

#pragma mark - 点击事件
// 发送被点击
- (void)action_sendButton11:(UIButton *)sender {
    if (self.commentDelegate) {
        [self.commentDelegate sendButtonClick];
    }
    self.commentTextView.textView.text = nil;
}
#pragma mark - UITextView 协议方法
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        self.send.userInteractionEnabled = NO;
        self.send.backgroundColor = [UIColor grayColor];
    }else{
        self.send.userInteractionEnabled = YES;
        self.send.backgroundColor = [UIColor colorWithRed:0.0616 green:0.3368 blue:0.7441 alpha:1.0];
    }

}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [textView resignFirstResponder];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    NSString * toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    if (toBeString.length >= 200 && range.length!= 1 ){
        textView.text = [toBeString substringToIndex:200];
        return NO;
        
    }
    return YES;
}

#pragma mark - Getters
- (CBTextView *)commentTextView{
    if (_commentTextView == nil){
        _commentTextView = [[CBTextView alloc]initWithFrame:CGRectMake(_width * 0.025, _height * 0.08, _width * 0.95, _height * 0.6 ) leftAndRigSpace:0];
        _commentTextView.aDelegate = self;
        _commentTextView.textView.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
        _commentTextView.placeHolder = @"优质内容会被优先展示";
        _commentTextView.textView.textColor = [UIColor blackColor];
        _commentTextView.placeHolderColor = [UIColor colorWithRed:0.7916 green:0.7916 blue:0.7916 alpha:1.0];
        _commentTextView.backgroundColor = [UIColor whiteColor];
        _commentTextView.layer.masksToBounds = YES;
        _commentTextView.layer.cornerRadius = 5 * [UIScreen zj_screenWScale];
        _commentTextView.backgroundColor = [UIColor randomColor];
        _commentTextView.aDelegate = self;
    }
    return _commentTextView;
}
- (UIButton *)send{
    if (_send == nil){
        _send = [[UIButton alloc]initWithFrame:CGRectMake(_width * 0.8, CGRectGetMaxY(self.commentTextView.frame) + _height * 0.035, _width * 0.15, _height * 0.25)];
        [_send setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_send setTitle:@"发表" forState:UIControlStateNormal];
        _send.titleLabel.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
        [_send addTarget:self action:@selector(action_sendButton11:) forControlEvents:UIControlEventTouchUpInside];
        _send.backgroundColor = [UIColor grayColor];
        _send.userInteractionEnabled = NO;
        _send.layer.masksToBounds = YES;
        _send.layer.cornerRadius = 5 * [UIScreen zj_screenHScale];
    }
    return _send;
}

- (void)setTishiString:(NSString *)tishiString{
    _tishiString = tishiString;
    self.commentTextView.placeHolder = tishiString;
}

- (void)setButtonString:(NSString *)buttonString{
    _buttonString = buttonString;
    [self.send setTitle:buttonString forState:UIControlStateNormal];
}



@end
